# 1.导包
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score              # 因为逻辑回归实际是解决分类问题的，因此准确度依然还可以用

# 2.读取数据
df = pd.read_csv("./breast-cancer-wisconsin.csv",encoding='utf-8')

# 3.数据基本处理

# 3.1填充缺失值（?填充）
df.replace("?", np.NAN, inplace=True)

# 3.2删除空值
df.dropna(inplace=True)

# 4.划分数据集
x_data = df.iloc[:, 1:-1]
y_data = df.iloc[:, -1]
x_train, x_test, y_train, y_test = train_test_split(x_data, y_data, test_size=0.2, random_state=922)

# 5.特征工程
# 5.1 特征预处理(标准化处理)
transformer = StandardScaler()
x_train = transformer.fit_transform(x_train)

# 6.模型构建
model = LogisticRegression()  # 逻辑回归算法
model.fit(x_train, y_train)

# 7.模型评估
# 7.1 对测试集进行标准化
x_test = transformer.transform(x_test)
y_predict = model.predict(x_test)           # 根据测试集的特征值 得到 预测值 y_predict
# result = accuracy_score(y_test, y_predict)
# print(result)
print(f"预测准确度为：{accuracy_score(y_test, y_predict)}")    # 使用真实的目标值和 预测值进行对比
